﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using System.Windows.Threading;

namespace TED
{
    public partial class Video : PhoneApplicationPage
    {
        DispatcherTimer currentPosition = new DispatcherTimer();

        public Video()
        {
            InitializeComponent();
            this.Loaded += new RoutedEventHandler(Video_Loaded);

        }

        void Video_Loaded(object sender, RoutedEventArgs e)
        {
            TEDVideoElement.MediaOpened += new RoutedEventHandler(TEDVideoElement_MediaOpened);
            TEDVideoElement.MediaEnded += new RoutedEventHandler(TEDVideoElement_MediaEnded);
            TEDVideoElement.CurrentStateChanged += new RoutedEventHandler(TEDVideoElement_CurrentStateChanged);
            currentPosition.Tick += new EventHandler(currentPosition_Tick);

            TEDVideoElement.Source = new Uri("Bear.wmv", UriKind.Relative);
        }

        void TEDVideoElement_MediaOpened(object sender, RoutedEventArgs e)
        {
            pbVideo.Maximum = (int)TEDVideoElement.NaturalDuration.TimeSpan.TotalMilliseconds;
            TEDVideoElement.Play();
        }

        void TEDVideoElement_MediaEnded(object sender, RoutedEventArgs e)
        {
            TEDVideoElement.Stop();
        }

        void TEDVideoElement_CurrentStateChanged(object sender, RoutedEventArgs e)
        {
            if (TEDVideoElement.CurrentState == MediaElementState.Playing)
            {
                currentPosition.Start();
                ((ApplicationBarIconButton)ApplicationBar.Buttons[0]).IsEnabled = false; // play
                ((ApplicationBarIconButton)ApplicationBar.Buttons[1]).IsEnabled = true;  // pause
                ((ApplicationBarIconButton)ApplicationBar.Buttons[2]).IsEnabled = true;  // stop

            }
            else if (TEDVideoElement.CurrentState == MediaElementState.Paused)
            {
                currentPosition.Stop();
                ((ApplicationBarIconButton)ApplicationBar.Buttons[0]).IsEnabled = true; // play
                ((ApplicationBarIconButton)ApplicationBar.Buttons[1]).IsEnabled = false;  // pause
                ((ApplicationBarIconButton)ApplicationBar.Buttons[2]).IsEnabled = true;  // stop
            }
            else
            {
                currentPosition.Stop();
                ((ApplicationBarIconButton)ApplicationBar.Buttons[0]).IsEnabled = true; // play
                ((ApplicationBarIconButton)ApplicationBar.Buttons[1]).IsEnabled = false;  // pause
                ((ApplicationBarIconButton)ApplicationBar.Buttons[2]).IsEnabled = false;  // stop

            }
        }

        void currentPosition_Tick(object sender, EventArgs e)
        {
            pbVideo.Value = (int)TEDVideoElement.Position.TotalMilliseconds;

        }

        private void Play_Click(object sender, EventArgs e)
        {
            TEDVideoElement.Play();
        }

        private void Pause_Click(object sender, EventArgs e)
        {
            TEDVideoElement.Pause();
        }

        private void AddFavorite_Click(object sender, EventArgs e)
        {
            TEDVideoElement.Stop();
        }
    }
}